home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Examples / aux / samples / copy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  3.1 KB  |  187 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include "tk.h"
  7.  
  8. #if defined(__MWERKS__) || defined(__SC__)
  9.     #include <console.h>
  10. #endif
  11.  
  12.  
  13. GLenum doubleBuffer, directRender;
  14. GLint windW, windH;
  15.  
  16. char *fileName = 0;
  17. TK_RGBImageRec *image;
  18. float point[3];
  19. float zoom;
  20. GLint x, y;
  21.  
  22.  
  23. static void Init(void)
  24. {
  25.     glClearColor(0.0, 0.0, 0.0, 0.0);
  26.     glDisable(GL_DITHER);
  27.  
  28.     x = 0;
  29.     y = windH;
  30.     zoom = 1.0;
  31. }
  32.  
  33. static void Reshape(int width, int height)
  34. {
  35.  
  36.     windW = (GLint)width;
  37.     windH = (GLint)height;
  38.  
  39.     glViewport(0, 0, windW, windH);
  40.  
  41.     glMatrixMode(GL_PROJECTION);
  42.     glLoadIdentity();
  43.     gluOrtho2D(0, windW, 0, windH);
  44.     glMatrixMode(GL_MODELVIEW);
  45. }
  46.  
  47. static GLenum Key(int key, GLenum mask)
  48. {
  49.     switch (key) {
  50.         case TK_ESCAPE:
  51.             tkQuit();
  52.         case TK_Z:
  53.             zoom += 0.05;
  54.         break;
  55.         case TK_z:
  56.             zoom -= 0.05;
  57.             // if (zoom < 0.2) zoom = 0.2;
  58.         break;
  59.         default:
  60.         return GL_FALSE;
  61.     }
  62.     return GL_TRUE;
  63. }
  64.  
  65. static GLenum Mouse(int mouseX, int mouseY, GLenum button)
  66. {
  67.     x = (GLint)mouseX;
  68.     y = (GLint)mouseY;
  69.     return GL_TRUE;
  70. }
  71.  
  72. static void Draw(void)
  73. {
  74.     glClear(GL_COLOR_BUFFER_BIT);
  75.  
  76.     point[0] = (windW / 2) - (image->sizeX / 2);
  77.     point[1] = (windH / 2) - (image->sizeY / 2);
  78.     point[2] = 0;
  79.     glRasterPos3fv(point);
  80.  
  81.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  82.     glPixelZoom(1.0, 1.0);
  83.     glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
  84.      image->data);
  85.  
  86.     point[0] = (float)x;
  87.     point[1] = windH - (float)y;
  88.     point[2] = 0.0;
  89.     glRasterPos3fv(point);
  90.  
  91.     glPixelZoom(zoom, zoom);
  92.     glCopyPixels((windW/2)-(image->sizeX/2),
  93.         (windH/2)-(image->sizeY/2),
  94.         image->sizeX, image->sizeY, GL_COLOR);
  95.  
  96.     aglSwapBuffers(aglGetCurrentContext());
  97. }
  98.  
  99. static GLenum Args(int argc, char **argv)
  100. {
  101.     GLint i;
  102.  
  103.     doubleBuffer = GL_TRUE;
  104.     directRender = GL_TRUE;
  105.  
  106.     for (i = 1; i < argc; i++)
  107.     {
  108.         if (strcmp(argv[i], "-sb") == 0)
  109.         {
  110.             doubleBuffer = GL_FALSE;
  111.         }
  112.         else if (strcmp(argv[i], "-db") == 0)
  113.         {
  114.             doubleBuffer = GL_TRUE;
  115.         }
  116.         else if (strcmp(argv[i], "-dr") == 0)
  117.         {
  118.             directRender = GL_TRUE;
  119.         }
  120.         else if (strcmp(argv[i], "-ir") == 0)
  121.         {
  122.             directRender = GL_FALSE;
  123.         }
  124.         else if (strcmp(argv[i], "-f") == 0)
  125.         {
  126.             if (i+1 >= argc || argv[i+1][0] == '-')
  127.             {
  128.                 printf("-f (No file name).\n");
  129.                 return GL_FALSE;
  130.             }
  131.             else
  132.             {
  133.                 fileName = argv[++i];
  134.             }
  135.         }
  136.         else
  137.         {
  138.             printf("%s (Bad option).\n", argv[i]);
  139.             return GL_FALSE;
  140.         }
  141.     }
  142.     
  143.     return GL_TRUE;
  144. }
  145.  
  146. void main(int argc, char **argv)
  147. {
  148.     GLenum type;
  149.     
  150.     #if defined(__MWERKS__) || defined(__SC__)
  151.         argc = ccommand(&argv);
  152.     #endif
  153.  
  154.     if(Args(argc, argv) == GL_FALSE)
  155.     {
  156.         tkQuit();
  157.     }
  158.  
  159.     if(fileName)
  160.         image = tkRGBImageLoad(fileName);
  161.     else
  162.         image = tkRGBImageLoad("1.rgb");
  163.  
  164.     windW = 300;
  165.     windH = 300;
  166.     tkInitPosition(30, 60, windW, windH);
  167.  
  168.     type = TK_RGB;
  169.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  170.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  171.     tkInitDisplayMode(type);
  172.  
  173.     if(tkInitWindow("Copy Test") == GL_FALSE)
  174.     {
  175.         tkQuit();
  176.     }
  177.  
  178.     Init();
  179.  
  180.     tkExposeFunc(Reshape);
  181.     tkReshapeFunc(Reshape);
  182.     tkKeyDownFunc(Key);
  183.     tkMouseDownFunc(Mouse);
  184.     tkDisplayFunc(Draw);
  185.     tkExec();
  186. }
  187.